package com.leoman.service.impl;

import com.leoman.core.Constant;
import com.leoman.dao.RecordInfoDao;
import com.leoman.entity.RecordInfo;
import com.leoman.entity.vo.StatData;
import com.leoman.service.RecordInfoService;
import com.leoman.utils.DateUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * RecordInfoServiceImpl
 * Created by 涂奕恒 on 2016/10/31 0031 下午 12:01.
 */
@Service
public class RecordInfoServiceImpl implements RecordInfoService {

    @Autowired
    private RecordInfoDao recordInfoDao;

    @Override
    public List<RecordInfo> findAll() {
        return recordInfoDao.findAll();
    }

    @Override
    public Page<RecordInfo> find(int pageNum, int pageSize) {
        return recordInfoDao.findAll(new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));
    }

    @Override
    public Page<RecordInfo> find(int pageNum) {
        return find(pageNum, Constant.PAGE_DEF_SIZE);
    }

    @Override
    public RecordInfo getById(int id) {
        return recordInfoDao.findOne(id);
    }

    @Override
    public void deleteById(int id) {
        recordInfoDao.delete(id);
    }

    @Override
    public RecordInfo create(RecordInfo recordInfo) {
        return recordInfoDao.save(recordInfo);
    }

    @Override
    public RecordInfo update(RecordInfo recordInfo) {
        return recordInfoDao.save(recordInfo);
    }

    @Override
    @Transactional
    public void deleteAll(int[] ids) {
        for (int id : ids) {
            deleteById(id);
        }
    }

    @Override
    public StatData getDataByDay(Integer day) {
        List<String> dayList = new ArrayList<>();
        List<Integer> videoClickNumList = new ArrayList<>();
        List<Integer> userGrowNumList = new ArrayList<>();
        RecordInfo recordInfo;
        Long startTime; // 当天的开始时间
        String dayDate;   // 当天的结束时间

        StatData statData = new StatData();
        statData.setDay(day);

        try {
            // 1、获取所有日期数据
            // 2、根据日期数据循环获取每天的视频点击量
            // 3、根据日期数据循环获取每天的用户增长量
            for (int i = day; i > 0; i--) {
                startTime = DateUtils.getDateWithDay((i - 1) * -1, 0).getTime();
                dayDate = DateUtils.longToStringWithFormat(startTime, "yyyy-MM-dd");

                // 将日期放入日期数据集合中
                dayList.add(dayDate.substring(5));

                recordInfo = recordInfoDao.findOneByRecordDate(dayDate);

                if (null == recordInfo) {
                    recordInfo = new RecordInfo();
                    recordInfo.setRecordDate(dayDate);
                    recordInfoDao.save(recordInfo);
                }

                // 查询当天的视频点击量，并放入视频点击量数据集合中
                videoClickNumList.add(recordInfo.getVideoPlayNum());

                // 查询当天的用户增长量，并放入用户增长量数据集合中
                userGrowNumList.add(recordInfo.getUserGrowNum());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        statData.setDayList(dayList);
        statData.setVideoClickNumList(videoClickNumList);
        statData.setUserGrowNumList(userGrowNumList);

        return statData;
    }

    @Override
    public void dataGrowWithDate(String dayDate, Integer videoPlayNum, Integer userGrowNum) {
        if (StringUtils.isBlank(dayDate)) {
            dayDate = DateUtils.longToStringWithFormat(System.currentTimeMillis(), "yyyy-MM-dd");
        }
        if (null == videoPlayNum) {
            videoPlayNum = 0;
        }
        if (null == userGrowNum) {
            userGrowNum = 0;
        }

        RecordInfo recordInfo = recordInfoDao.findOneByRecordDate(dayDate);

        if (null == recordInfo) {
            recordInfo = new RecordInfo();
            recordInfo.setRecordDate(dayDate);
        }

        recordInfo.setVideoPlayNum(recordInfo.getVideoPlayNum() + videoPlayNum);
        recordInfo.setUserGrowNum(recordInfo.getUserGrowNum() + userGrowNum);

        recordInfoDao.save(recordInfo);
    }
}